# Create a numeric object
my_number = 5.6
# Check the class
class(my_number)[1] "numeric"
# Perform a calculation
my_number + 5[1] 10.6
Jeremy Springman
University of Pennsylvania
July 23, 2024
Before using R to illustrate basic programming concepts and data analysis tools, we will get familiar with the RStudio layout.
RStudio has four primary panels that will help you interact with your data. We will use the default layout of these panels.
Let’s use these panels to create and interact with data.
Console:
2 + 2 into the console panel and hit ENTERsum = 2 + 2 into the console panel and hit ENTERSource:
sum = 2 + 3 into the source panel and hit cntrl+ENTEREnvironment:
sum is stored in our environmentrm(sum) to clear the object from the environmentViewer:
Now that we understand the layout, we are ready to review the concepts covered in Module 2 Week 2.2. These concepts will help us understand what is happening when we create and manipulate data.
“Object” is a generic term for anything that R stores in the environment. This can include anything from an individual number or word, to lists of values, to entire datasets.
Importantly, objects belong to different “classes” depending on the type of values that they store.
[1] "numeric"
[1] 10.6
The class of an object determines the type of operations you can perform on it. Some operations can only be run on numeric objects (numbers).
R contains functions that can convert some objects to different factors.
[1] "numeric"
[1] "numeric"
Lists
c()# Create a numeric vector
numeric_vector = c(6, 11, 13, 31)
# Print the vector
print(numeric_vector)[1] 6 11 13 31
[1] "numeric"
[1] 15.25
[1] "character"
[1] NA
"hello world" and "welcome to R"TRUE or FALSE==. For example, 2 + 1 == 3 will return TRUE< (less than), <= (less than or equal), and !=(not equal to). For example, 3 + 5 <= 1 will return FALSE& represent “and” while | represents “or.” For example, (2 + 1 == 3) & (2 + 1 == 4) returns FALSE since both clauses are not TRUEinstall.packages() to install a package
dplyr package.csv fileread.csv() to pull data from a spreadsheet on your harddrive into your R/RStudio environment
.csv file is stored
Year I Year II Year III
327 277 221
0 1
327 221
| Bivariate | Multivariate | Interaction | |
|---|---|---|---|
| (Intercept) | −0.150*** (0.036) | −0.142** (0.051) | −0.184** (0.061) |
| moved | 0.234*** (0.045) | 0.273*** (0.055) | 0.334*** (0.073) |
| year | −0.046 (0.055) | 0.038 (0.086) | |
| moved × year | −0.142 (0.112) | ||
| Num.Obs. | 809 | 534 | 534 |
| R2 Adj. | 0.032 | 0.045 | 0.046 |
0 1 2
327 277 221
| Bivariate | Multivariate | Interaction | |
|---|---|---|---|
| (Intercept) | −0.150*** (0.036) | −0.121** (0.045) | −0.169** (0.056) |
| moved | 0.234*** (0.045) | 0.228*** (0.045) | 0.301*** (0.067) |
| year | −0.029 (0.027) | 0.019 (0.042) | |
| moved × year | −0.080 (0.054) | ||
| Num.Obs. | 809 | 809 | 809 |
| R2 Adj. | 0.032 | 0.032 | 0.033 |
0 1 2
327 277 221
| Bivariate | Multivariate | Interaction | |
|---|---|---|---|
| (Intercept) | −0.150*** (0.036) | −0.114* (0.046) | −0.184** (0.060) |
| moved | 0.234*** (0.045) | 0.232*** (0.045) | 0.334*** (0.072) |
| year2 | −0.060 (0.049) | 0.077 (0.092) | |
| year3 | −0.053 (0.054) | 0.038 (0.085) | |
| moved × year2 | −0.194+ (0.109) | ||
| moved × year3 | −0.142 (0.110) | ||
| Num.Obs. | 809 | 809 | 809 |
| R2 Adj. | 0.032 | 0.031 | 0.033 |
| Bivariate | Multivariate | Interaction | |
|---|---|---|---|
| (Intercept) | −0.150*** (0.026) | −0.148*** (0.030) | −0.150*** (0.037) |
| moved | 0.232*** (0.032) | 0.232*** (0.032) | 0.234*** (0.046) |
| time | −0.004 (0.031) | −0.001 (0.052) | |
| moved × time | −0.004 (0.065) | ||
| Num.Obs. | 1634 | 1634 | 1634 |
| R2 Adj. | 0.030 | 0.030 | 0.029 |
| Simple | Fixed Effects | Interaction | |
|---|---|---|---|
| (Intercept) | 0.005 (0.022) | −0.330 (0.332) | −0.374 (0.333) |
| time | −0.005 (0.031) | −0.002 (0.023) | 0.000 (0.040) |
| moved | 0.044 (0.470) | ||
| moved × time | −0.004 (0.049) | ||
| Num.Obs. | 1634 | 1634 | 1634 |
| R2 Adj. | −0.001 | 0.442 | 0.442 |
Some operations can only be run on character (string) objects.